<?php


require_once('config.php');
$mysqli = new mysqli($db['host'], $db['user'], $db['pass'], $db['db']);
$mysqli->set_charset($db['enc']);

require_once(dirname(__FILE__) . '/../taxamatch/api/config.php');
require_once(dirname(__FILE__) . "/../taxamatch/api/classes/class.taxamatch.php");
$TaxaMatch = new Taxamatch();

$matched_canonical_forms = array();
$matched_name_strings = array();


$result = $mysqli->query("SELECT id, word1, word2, word3 FROM extended_canonical_forms WHERE number_of_words=2 AND word1 LIKE 'Q%' order by word1");
while($result && $row=$result->fetch_assoc())
{
	$words = array();
	if($row['word1']) $words[] = $row['word1'];
	else continue;
	if($row['word2']) $words[] = $row['word2'];
	//if($row['word3']) $words[] = $row['word3'];
	
	process_canonical_form($row['id'], $words);
	print_r($words);
}


//process_canonical_form(56298, array('Pomatomus', 'saltator'));


function process_canonical_form($id, $words)
{
	global $mysqli;
	global $TaxaMatch;
	global $matched_canonical_forms;
	global $matched_name_strings;
	
	$query = "SELECT id match_id FROM extended_canonical_forms WHERE number_of_words=".count($words)." AND id!=$id AND";
	$i = 1;
	foreach($words as $word)
	{
		if($i>3) break;
		if($i>1) $query .= " AND";
		
		$lengths = lengths($word);

		$query .= " word".$i." LIKE '".substr($word, 0, 1)."%' AND word".$i."_length BETWEEN ".$lengths['min']." AND ".$lengths['max'];
		$i++;
	}
	//profile("$query");
	$result = $mysqli->query($query);
	//profile("done query");
	while($result && $row=$result->fetch_assoc())
	{
		compare_canonical_forms($id, $row['match_id']);
	}
}

function compare_canonical_forms($id1, $id2)
{
	global $mysqli;
	global $TaxaMatch;
	global $matched_canonical_forms;
	global $matched_name_strings;
	
	if(@$matched_canonical_forms[$id1.'|'.$id2]) return;
	
	if(!$id1 || !$id2 || $id1 == $id2) return;
	//profile("getting name strings");
	$names = get_name_strings_with_canonical_form_ids(array($id1, $id2));
	//profile("got name strings");
	$names_clone = $names;
	
	foreach($names as $name1)
	{
		$nid1 = $name1['id'];
		$string1 = $name1['name'];
		foreach($names_clone as $name2)
		{
			$nid2 = $name2['id'];
			$string2 = $name2['name'];
			if($nid1 == $nid2) continue;
			if(@$matched_name_strings[$nid1.'|'.$nid2]) continue;
			
			//echo "Comparing $string1 and $string2: ";
			$obj1 = get_string_object($nid1);
			$obj2 = get_string_object($nid2);
			if(!($obj1 && $obj2)) continue;
			$result = $TaxaMatch->name_objects_match($obj1, $obj2);
			
			if($result)
			{
				echo "$string1\n";
				echo "$string2\n";
				echo "$result\n\n";
			}
			
			$matched_name_strings[$nid1.'|'.$nid2] = 1;
			$matched_name_strings[$nid2.'|'.$nid1] = 1;
		}
	}
	
	$matched_canonical_forms[$id1.'|'.$id2] = 1;
	$matched_canonical_forms[$id2.'|'.$id1] = 1;
}

function get_string_object($name_string_id){
	global $mysqli;
	global $TaxaMatch;
	global $matched_canonical_forms;
	global $matched_name_strings;

	$return = new stdClass;
	$return->authors_years = array('authors' => array(), 'years' => array());
	$query = "select nw.word, sm.name from name_word_semantics nws join name_words nw on nw.id = nws.name_word_id join semantic_meanings sm on sm.id = nws.semantic_meaning_id where nws.name_string_id = $name_string_id";
	$res = $mysqli->query($query);
	while($res && $row = $res->fetch_assoc()) {
		$sem = $row['name'];
		if ($sem == 'genus') $return->genus = $row['word'];
		elseif ($sem == 'species') $return->species = $row['word'];
		elseif ($sem == 'author_word') $return->authors_years['authors'][] = $row['word'];
		elseif ($sem == 'year') $return->authors_years['years'][] = $row['word'];
	}
	if (!(isset($return->genus) && isset($return->species))) return null;
	return $return;
}

function get_name_strings_with_canonical_form_ids($ids)
{
	global $mysqli;
		
	$names = array();
	
	$result = $mysqli->query("SELECT id, name FROM name_strings WHERE canonical_form_id IN (".implode(",", $ids).")");
	while($result && $row=$result->fetch_assoc())
	{
		$names[] = $row;
	}
	
	return $names;
}


function lengths($word) {
  $length = strlen($word);
  $lengths = array();
  $delta =  round($length/5);
  $lengths['min'] = $length - $delta; 
  $lengths['max'] = $length + $delta; 
  return $lengths;
}


?>